Skip to content

[Python] Fix handling of zero-width types returned by a custom Demangler#8100

Merged
bdash merged 1 commit intodevfrom
test_python_demangler_type
Apr 17, 2026
Merged

[Python] Fix handling of zero-width types returned by a custom Demangler#8100
bdash merged 1 commit intodevfrom
test_python_demangler_type

Conversation

@bdash
Copy link
Copy Markdown
Contributor

@bdash bdash commented Apr 16, 2026

Explicitly test the type against None rather than testing for truthiness. This ensures that zero-width types returned by a custom demangler will be applied.

Fixes #8099.


For some reason Type implements __len__ and returns self.width. This causes zero-width types, such as function types, to be considered False. This is somewhere on the spectrum of unnecessarily confusing and just plain wrong.

@bdash bdash requested a review from CouleeApps April 16, 2026 04:19
Copy link
Copy Markdown
Member

@CouleeApps CouleeApps left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... I'm surprised python makes the leap from truthiness -> non zero length. What ever happened to "Explicit is better than implicit."

The bugfix change itself is good but the test you wrote (in the private repo) might be better placed in test_demangler.py and likely doesn't need all the extra scaffolding.

@bdash
Copy link
Copy Markdown
Contributor Author

bdash commented Apr 16, 2026

I'll rework my test. I somehow entirely overlooked the existence of that file 😅

Python has treated empty lists as false since very early versions, and they expanded that behavior to user defined types via __len__. I think we could implement __bool__ on Type to avoid this, but I'm not entirely sure why it implements __len__. It's not a sequence or a mapping as Python sees it. len(some_type) evaluating to its width seems kinda surprising, and I'm not sure why anyone would reach for that vs some_type.width. It may be too late to undo that now, though.

@CouleeApps
Copy link
Copy Markdown
Member

I don't think we should make any changes to Type at this time, but it would be worth looking around the codebase to see if anywhere else accidentally (or intentionally?) makes use of this behavior

@bdash bdash force-pushed the test_python_demangler_type branch from 9304762 to 424dc72 Compare April 17, 2026 01:15
@bdash bdash merged commit 424dc72 into dev Apr 17, 2026
1 check passed
@bdash bdash deleted the test_python_demangler_type branch April 17, 2026 01:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Type returned by custom demangler written in Python is not applied

2 participants